package timing.ukulele.portal.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import timing.ukulele.data.portal.view.SysResourceVO;
import timing.ukulele.portal.convertor.ResourceConvertor;
import timing.ukulele.portal.mapper.SysResourceMapper;
import timing.ukulele.portal.persistent.SysResource;

import java.util.List;

@Service
public class SysResourceService extends BaseOperatorService<SysResourceMapper, SysResource> {

    public IPage<SysResourceVO> getPage(Long groupId, String resource, String remark, String method, Boolean deleted, int current, int size) {
        Page<SysResource> page = new Page<>(current, size);
        LambdaQueryChainWrapper<SysResource> query = this.lambdaQuery();
        query.eq(groupId != null, SysResource::getGroupId, groupId);
        query.eq(StringUtils.hasText(resource), SysResource::getResource, resource);
        query.eq(StringUtils.hasText(remark), SysResource::getRemark, remark);
        query.eq(StringUtils.hasText(method), SysResource::getMethod, method);
        query.eq(deleted != null, SysResource::getDeleted, deleted);
        query.orderByDesc(SysResource::getId);
        IPage<SysResource> iPage = query.page(page);
        Page<SysResourceVO> voPage = new Page<>(current, size);
        if (iPage != null && !CollectionUtils.isEmpty(iPage.getRecords())) {
            List<SysResourceVO> voList = ResourceConvertor.INSTANCE.p2vList(iPage.getRecords());
            voPage.setRecords(voList);
            voPage.setTotal(iPage.getTotal());
        }
        return voPage;
    }
}
